<template>
  <t-form ref="form" class="base-form" :data="formData" :rules="ADMIN_PASSWORD_RULES" label-align="left" :label-width="100"
    @submit="onSubmit">
    <t-form-item label="新密码">
      <t-input v-model="formData.password1" placeholder="" type="password"></t-input>
    </t-form-item>
    <t-form-item label="再输一次">
      <t-input v-model="formData.password2" placeholder="" type="password"></t-input>
    </t-form-item>
    <t-form-item>
      <t-button theme="primary" class="form-submit-confirm" type="submit"> 确认提交 </t-button>
      <!-- <t-button type="reset" class="form-submit-cancel" theme="default" variant="base" @click="onCancel"> 取消
      </t-button> -->
    </t-form-item>

  </t-form>
</template>

<script setup lang="ts">
  import {
    MessagePlugin
  } from 'tdesign-vue-next';
  import {
    ref, defineEmits
  } from 'vue';
  import {
    ADMIN_PASSWORD,
    ADMIN_PASSWORD_RULES
  } from './constants';
  import {passwordChange} from '@/api/user'

  const emits = defineEmits(['close'])
  const formData = ref({
    ...ADMIN_PASSWORD
  });

  const onSubmit = ({validateResult,firstError}) => {
    if (validateResult === true) {
      saveData();

      emits('close')
    }
  };
  const onCancel = () => {
    MessagePlugin.error('己取消')
    emits('close')
  }

  const saveData=()=>{
      passwordChange(formData.value)
      .then(ret=>{
        MessagePlugin.success('修改成功')
        emits('close')
      })
      .catch(e=>{
        if(e.message)
        {
          MessagePlugin.error(e.message)
        }else
        {
          MessagePlugin.error(e.msg)
        }

      })
  }

</script>

<style>
</style>
